<?php
require 'vendor/autoload.php';
class Cosup
{
	public static function cosupload($config,$file) {
		$cosClient = new Qcloud\Cos\Client(array(
		    'region' => $config['region'],
		    'credentials' => array(
		        'appId' => $config['appid'],
		        'secretId' => $config['secretid'],
		        'secretKey' => $config['secretkey'],
		    ),
		));
		
		// 若初始化 Client 时未填写 appId，则 bucket 的命名规则为{name}-{appid} ，此处填写的存储桶名称必须为此格式
		if($file['name']=='blob'){
			$upload_name= 'dome.jpg';
		}else{
			$upload_name = $file['name']; 
		}
	
		$str="0123456789abcdefghijklmnopqrstuvwxyz";
	    $key = "";	
	    for($i=0;$i<4;$i++){
	        $key .= $str{mt_rand(0,32)};
	    }
		$type = "image";
		$key = 'sxkj_offline/'.$type.'/'.uniqid().$key.strrchr($upload_name, '.');
		$local_path = $file["tmp_name"];
		
		# 上传文件
		## putObject(上传接口，最大支持上传5G文件)
		### 上传内存中的字符串
		try {
			$result = $cosClient->putObject(array(
			    'Bucket' => $config['bucket'],
			    'Key' => $key,
			    'Body' => fopen($local_path, 'rb')
			));
		    
		    $arr = (array)$result;		
			foreach ($arr as $v){
				$array = $v;
			}

		    return $array['ObjectURL'];
		} catch (\Exception $e) {
		    return $e;
		}	
	}			
}



























